{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "outputs": [],
   "source": [
    "import torch"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-05-14T08:17:42.119727200Z",
     "start_time": "2024-05-14T08:17:40.882621600Z"
    }
   },
   "id": "f6362e7d6df4eb12"
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "initial_id",
   "metadata": {
    "collapsed": true,
    "ExecuteTime": {
     "end_time": "2024-05-14T08:17:42.125728300Z",
     "start_time": "2024-05-14T08:17:42.120727100Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": "tensor([[1, 2, 3],\n        [2, 0, 4],\n        [3, 4, 5]])"
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "B = torch.tensor([[1, 2, 3], [2, 0, 4], [3, 4, 5]])\n",
    "B"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "outputs": [
    {
     "data": {
      "text/plain": "tensor([[True, True, True],\n        [True, True, True],\n        [True, True, True]])"
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "B == B.T"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-05-14T08:17:42.143567700Z",
     "start_time": "2024-05-14T08:17:42.125728300Z"
    }
   },
   "id": "8b6b1062fad80b00"
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "outputs": [
    {
     "data": {
      "text/plain": "(tensor([[ 0.,  1.,  2.,  3.],\n         [ 4.,  5.,  6.,  7.],\n         [ 8.,  9., 10., 11.],\n         [12., 13., 14., 15.],\n         [16., 17., 18., 19.]]),\n tensor([[ 0.,  1.,  2.,  3.],\n         [ 4.,  5.,  6.,  7.],\n         [ 8.,  9., 10., 11.],\n         [12., 13., 14., 15.],\n         [16., 17., 18., 19.]]))"
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "A = torch.arange(20, dtype=torch.float32).reshape(5, 4)\n",
    "B = A.clone()  # 深复制\n",
    "A, B"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-05-14T08:17:42.178472700Z",
     "start_time": "2024-05-14T08:17:42.142567Z"
    }
   },
   "id": "ae21c34a3104b6c8"
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "outputs": [
    {
     "data": {
      "text/plain": "tensor([[  0.,   1.,   4.,   9.],\n        [ 16.,  25.,  36.,  49.],\n        [ 64.,  81., 100., 121.],\n        [144., 169., 196., 225.],\n        [256., 289., 324., 361.]])"
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "A * B  #内积 不是矩阵乘法"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-05-14T08:17:42.179473200Z",
     "start_time": "2024-05-14T08:17:42.152710200Z"
    }
   },
   "id": "651e5af358621e9c"
  },
  {
   "cell_type": "markdown",
   "source": [
    "按照指定的维度求和 和 torch.cat 方法类似"
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "b038d2efaae386f6"
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "tensor([[[ 0.,  1.,  2.,  3.,  4.],\n",
      "         [ 5.,  6.,  7.,  8.,  9.]],\n",
      "\n",
      "        [[10., 11., 12., 13., 14.],\n",
      "         [15., 16., 17., 18., 19.]]])\n",
      "tensor(190.)\n"
     ]
    },
    {
     "data": {
      "text/plain": "tensor([[10., 12., 14., 16., 18.],\n        [20., 22., 24., 26., 28.]])"
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "A = A.reshape((2,2,5))\n",
    "print(A)\n",
    "A_sum_axis0 = A.sum(axis=0)\n",
    "A_sum_axis0"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-05-14T08:22:40.856264600Z",
     "start_time": "2024-05-14T08:22:40.846157200Z"
    }
   },
   "id": "5762c7b232d0ed69"
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "tensor(190.)\n",
      "tensor(9.5000)\n",
      "20\n"
     ]
    }
   ],
   "source": [
    "print(A.sum())\n",
    "print(A.mean())\n",
    "print(A.numel())"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-05-14T08:23:36.759944100Z",
     "start_time": "2024-05-14T08:23:36.745728800Z"
    }
   },
   "id": "716acc043411dd8e"
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "outputs": [
    {
     "data": {
      "text/plain": "tensor([[ 5.,  6.,  7.,  8.,  9.],\n        [10., 11., 12., 13., 14.]])"
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "A.mean(axis=0)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-05-14T08:24:01.329253800Z",
     "start_time": "2024-05-14T08:24:01.314628Z"
    }
   },
   "id": "b364e9e5ca3210d8"
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "outputs": [
    {
     "data": {
      "text/plain": "tensor([[[10., 12., 14., 16., 18.],\n         [20., 22., 24., 26., 28.]]])"
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "sum_a = A.sum(dim=0, keepdim=True)\n",
    "sum_a"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-05-14T08:34:29.152973800Z",
     "start_time": "2024-05-14T08:34:29.148493400Z"
    }
   },
   "id": "8a879d750d376a72"
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "outputs": [
    {
     "data": {
      "text/plain": "tensor([[[0.0000, 0.0833, 0.1429, 0.1875, 0.2222],\n         [0.2500, 0.2727, 0.2917, 0.3077, 0.3214]],\n\n        [[1.0000, 0.9167, 0.8571, 0.8125, 0.7778],\n         [0.7500, 0.7273, 0.7083, 0.6923, 0.6786]]])"
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "A / sum_a"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-05-14T08:34:36.653081900Z",
     "start_time": "2024-05-14T08:34:36.648575600Z"
    }
   },
   "id": "4a14a6cb4e11e89e"
  },
  {
   "cell_type": "markdown",
   "source": [
    "它会返回一个新的张量，其中的元素是原始张量沿着指定维度的累积和 下面的例子是一个三维的"
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "7232178a814f1426"
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "outputs": [
    {
     "data": {
      "text/plain": "tensor([[[ 0.,  1.,  3.,  6., 10.],\n         [ 5., 11., 18., 26., 35.]],\n\n        [[10., 21., 33., 46., 60.],\n         [15., 31., 48., 66., 85.]]])"
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "A.cumsum(dim=2)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-05-14T11:34:11.301870800Z",
     "start_time": "2024-05-14T11:34:11.289571600Z"
    }
   },
   "id": "4c6901580fdcb2ce"
  },
  {
   "cell_type": "markdown",
   "source": [
    "矩阵和向量的乘法 mv matrix vector multi"
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "a8a6f7fb9ec78660"
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "tensor([[1., 1., 1., 1.],\n",
      "        [1., 1., 1., 1.],\n",
      "        [1., 1., 1., 1.],\n",
      "        [1., 1., 1., 1.],\n",
      "        [1., 1., 1., 1.]])\n",
      "tensor([0.5568, 1.4490, 0.5673, 0.5429])\n",
      "tensor([3.1161, 3.1161, 3.1161, 3.1161, 3.1161])\n"
     ]
    }
   ],
   "source": [
    "Matrix_A = torch.ones((5,4))\n",
    "Vector_x = torch.randn((4,))\n",
    "print(Matrix_A)\n",
    "print(Vector_x)\n",
    "res = torch.mv(Matrix_A, Vector_x)\n",
    "print(res)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-05-14T11:45:59.035304500Z",
     "start_time": "2024-05-14T11:45:59.023090400Z"
    }
   },
   "id": "891cf0e05af6d13e"
  },
  {
   "cell_type": "markdown",
   "source": [
    "矩阵和矩阵的乘法 mm  matrix matrix"
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "bf9849768d38dd5a"
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "outputs": [
    {
     "data": {
      "text/plain": "tensor([[-1.1647, -0.0773, -2.2120,  0.5326,  2.8960],\n        [-1.1647, -0.0773, -2.2120,  0.5326,  2.8960],\n        [-1.1647, -0.0773, -2.2120,  0.5326,  2.8960],\n        [-1.1647, -0.0773, -2.2120,  0.5326,  2.8960],\n        [-1.1647, -0.0773, -2.2120,  0.5326,  2.8960]])"
     },
     "execution_count": 29,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "Matrix_B = torch.ones((5,4))\n",
    "Matrix_C = torch.randn((4,5))\n",
    "\n",
    "res = torch.mm(Matrix_B, Matrix_C)\n",
    "res"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-05-14T11:48:25.386977700Z",
     "start_time": "2024-05-14T11:48:25.373203500Z"
    }
   },
   "id": "33a5e0d5f533a063"
  },
  {
   "cell_type": "markdown",
   "source": [
    "求矩阵的 2范数 梦回矩阵论 torch.norm()"
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "e4956dc5f5f9685d"
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "outputs": [
    {
     "data": {
      "text/plain": "tensor(5.)"
     },
     "execution_count": 30,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "u = torch.tensor([3.0, -4.0])\n",
    "torch.norm(u)"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-05-14T11:49:47.268067600Z",
     "start_time": "2024-05-14T11:49:47.248030Z"
    }
   },
   "id": "97cae29bff0d8d60"
  },
  {
   "cell_type": "markdown",
   "source": [
    "求矩阵的 1范数 没有api 取绝对值求和"
   ],
   "metadata": {
    "collapsed": false
   },
   "id": "a2b8e72babf7a454"
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "outputs": [
    {
     "data": {
      "text/plain": "tensor(7.)"
     },
     "execution_count": 31,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "torch.abs(u).sum()"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "end_time": "2024-05-14T11:50:58.899584600Z",
     "start_time": "2024-05-14T11:50:58.888169600Z"
    }
   },
   "id": "5ea34ff1f6dd7aff"
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "outputs": [],
   "source": [],
   "metadata": {
    "collapsed": false
   },
   "id": "d52dcb6cc283445a"
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
